﻿using mars_client.csharp.enums;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Drawing;

namespace mars_client.csharp.util
{
    class TransformUtil
    {
        // 读取文件二进制流
        public static byte[] GetBytesFromFile(string file_path)
        {
            byte[] post_byte = null;
            // int startTime;
            using (FileStream fs = new FileStream(file_path, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    //  startTime = System.Environment.TickCount;
                    post_byte = br.ReadBytes((int)fs.Length);
                    fs.Close();
                }
            }
            //  int endTime = System.Environment.TickCount;
            //  int runTime = endTime - startTime;
            //  MessageBox.Show(runTime.ToString());
            return post_byte;
        }
        // 接收信息反序列化
        public static Object bytesToObject(byte[] bytes)
        {
            return JsonConvert.DeserializeObject(Encoding.GetEncoding("UTF-8").GetString(bytes));
        }
        // 发送信息序列化
        public static byte[] ObjectToBytes(Object o)
        {
            return Encoding.GetEncoding("UTF-8").GetBytes(JsonConvert.SerializeObject(o) + "$#$#");
        }
        // 将反序列化的对象转化
        public static T J2O<T>(Object o)
        {
            return ((JObject)o).ToObject<T>();
        }

        public static string Base64ImageToString(string Imagefilename)
        {
            String base64 = "";
            try
            {
                Bitmap bmp = new Bitmap(Imagefilename);
                FileStream fs = new FileStream(Imagefilename, FileMode.Create);

                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                base64 = Convert.ToBase64String(arr);
                
                fs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("ImgToBase64String 转换失败\nException:" + ex.Message);
            }

            return base64;
        }
        public static BitmapSource Base64StringToImage(string base64)
        {
            try
            {
                byte[] arr = Convert.FromBase64String(base64);
                MemoryStream ms = new MemoryStream(arr);
                Bitmap bmp = new Bitmap(ms);
                ms.Close();

                return BitmapToBitmapImage(bmp);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Base64StringToImage 转换失败\nException：" + ex.Message);
            }
            return null;
        }

        private static BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
        {
            BitmapImage bitmapImage = new BitmapImage();
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                bitmap.Save(ms, bitmap.RawFormat);
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = ms;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                bitmapImage.Freeze();
            }
            return bitmapImage;
        }
    }
}
